;======================================================================
;背景调色板更新
Bkg_Palette_Update
    .IF BKG_PAL_UPDATE_ENABLE
        LDA FC_Nmi_Task_Flags
        AND #TASK_PAL_UPDATE_ENABLE
        BEQ Bkg_Palette_Update.End
        DEC FC_Bkg_Pal_Time
        BNE Bkg_Palette_Update.End
        LDA #BKG_PAL_UPDATE_TIME
        STA FC_Bkg_Pal_Time
        LDA FC_Bkg_Pal_Index
        CMP #BKG_PAL_COUNT - 1
        BCC Bkg_Palette_Update.Write_Data
        LDA #$FF
        STA FC_Bkg_Pal_Index
Bkg_Palette_Update.Write_Data
        INC FC_Bkg_Pal_Index
        LDX #$00
        LDA FC_Bkg_Pal_Index
        ASL
        TAY
        LDA Bkg_Pal_Data_Index,Y
        STA FC_Data_Addr_L
        INY 
        LDA Bkg_Pal_Data_Index,Y
        STA FC_Data_Addr_H
        LDY #$00
        LDX #$10
Bkg_Palette_Update.Write
        LDA (FC_Data_Addr_L),Y
        STA FC_PPU_Bkg_Pal_Addr,Y
        INY
        DEX
        BNE Bkg_Palette_Update.Write
Bkg_Palette_Update.End
    .ENDIF
    RTS
    
;======================================================================
;精灵调色板更新
Sp_Palette_Update
    .IF SP_PAL_UPDATE_ENABLE
        LDA FC_Nmi_Task_Flags
        AND #TASK_PAL_UPDATE_ENABLE
        BEQ Sp_Palette_Update.End
        DEC FC_Sp_Pal_Time
        BNE Sp_Palette_Update.End
        LDA #SP_PAL_UPDATE_TIME
        STA FC_Sp_Pal_Time
        LDA FC_Sp_Pal_Index
        CMP #SP_PAL_COUNT - 1
        BCC Sp_Palette_Update.Write_Data
        LDA #$FF
        STA FC_Sp_Pal_Index
Sp_Palette_Update.Write_Data
        INC FC_Sp_Pal_Index
        LDX #$00
        LDA FC_Sp_Pal_Index
        ASL
        TAY
        LDA Sp_Pal_Data_Index,Y
        STA FC_Data_Addr_L
        INY 
        LDA Sp_Pal_Data_Index,Y
        STA FC_Data_Addr_H
        LDY #$00
        LDX #$10
Sp_Palette_Update.Write
        LDA (FC_Data_Addr_L),Y
        STA FC_PPU_Sp_Pal_Addr,Y
        INY
        DEX
        BNE Sp_Palette_Update.Write
Sp_Palette_Update.End
    .ENDIF
    RTS

;======================================================================
;CHR bank 缓冲更新
Chr_Update
    LDA FC_Nmi_Task_Flags
    AND #TASK_CHR_UPDATE_ENABLE
    BEQ Chr_Update.End
    DEC FC_Chr_Time
    BNE Chr_Update.End
    LDA #CHR_UPDATE_TIME
    STA FC_Chr_Time
    LDA FC_Chr_Index
    CMP #CHR_COUNT - 1
    BCC Chr_Update.Write_Data
    LDA #$FF
    STA FC_Chr_Index
Chr_Update.Write_Data
    INC FC_Chr_Index
    LDX #$00
    LDA FC_Chr_Index
    ASL
    TAY
    LDA Chr_Data_Index,Y
    STA FC_Data_Addr_L
    INY 
    LDA Chr_Data_Index,Y
    STA FC_Data_Addr_H
    LDY #$00
    LDX #$06
Chr_Update.Write
    LDA (FC_Data_Addr_L),Y
    STA FC_CHR_Buf_Addr,Y
    INY
    DEX
    BNE Chr_Update.Write
Chr_Update.End
    RTS

;======================================================================
;CHR bank 缓冲更新初始化
Chr_Update_Init
    LDA #$00
    STA FC_Chr_Index
    LDX #$00
    LDA FC_Chr_Index
    ASL
    TAY
    LDA Chr_Data_Index,Y
    STA FC_Data_Addr_L
    INY 
    LDA Chr_Data_Index,Y
    STA FC_Data_Addr_H
    LDY #$00
    LDX #$06
Chr_Update_Init.Write
    LDA (FC_Data_Addr_L),Y
    STA FC_CHR_Buf_Addr,Y
    INY
    DEX
    BNE Chr_Update_Init.Write
Chr_Update_Init.End
    RTS

;======================================================================
;背景调色板渐变更新
Bkg_Palette_Fade_Update
    PHA
    CMP #BKG_PAL_FADE_COUNT
    BCS Bkg_Palette_Fade_Update.End
    LDX #$00
    ASL
    TAY
    LDA Bkg_Pal_Fade_Index,Y
    STA FC_Data_Addr_L
    INY 
    LDA Bkg_Pal_Fade_Index,Y
    STA FC_Data_Addr_H
    LDY #$00
    LDX #$10
Bkg_Palette_Fade_Update.Write
    LDA (FC_Data_Addr_L),Y
    STA FC_PPU_Bkg_Pal_Addr,Y
    INY
    DEX
    BNE Bkg_Palette_Fade_Update.Write
Bkg_Palette_Fade_Update.End
    PLA
    RTS

;======================================================================
;精灵调色板渐变更新
Sp_Palette_Fade_Update
    PHA
    CMP #SP_PAL_FADE_COUNT
    BCS Sp_Palette_Fade_Update.End
    LDX #$00
    ASL
    TAY
    LDA Sp_Pal_Fade_Index,Y
    STA FC_Data_Addr_L
    INY 
    LDA Sp_Pal_Fade_Index,Y
    STA FC_Data_Addr_H
    LDY #$00
    LDX #$10
Sp_Palette_Fade_Update.Write
    LDA (FC_Data_Addr_L),Y
    STA FC_PPU_Sp_Pal_Addr,Y
    INY
    DEX
    BNE Sp_Palette_Fade_Update.Write
Sp_Palette_Fade_Update.End
    PLA
    RTS

;======================================================================
;调色板渐入处理
Fade_In_Process
    LDA #$00
    STA <FC_Data_Count
Fade_In_Process.Update
    LDA <FC_Data_Count
    JSR Bkg_Palette_Fade_Update
    JSR Sp_Palette_Fade_Update
    LDA #FADE_IN_TIME
    JSR Wait_For_Time_Delay
    INC <FC_Data_Count
    LDA <FC_Data_Count
    CMP #BKG_PAL_FADE_COUNT
    BCC Fade_In_Process.Update
Fade_In_Process.End
    RTS

;======================================================================
;调色板渐出处理
Fade_Out_Process
    LDA #BKG_PAL_FADE_COUNT - 1
    STA <FC_Data_Count
Fade_Out_Process.Update
    LDA <FC_Data_Count
    JSR Bkg_Palette_Fade_Update
    JSR Sp_Palette_Fade_Update
    LDA #FADE_OUT_TIME
    JSR Wait_For_Time_Delay
    DEC <FC_Data_Count
    BPL Fade_Out_Process.Update
Fade_Out_Process.End
    RTS
